<?php
class OrderController {
	public function lists(){
		$where  =$_GET;
		$data = HttpServerManager::getOrderServer()->lists($where);
		$where = array();
		$where['year'] = date('Y');
		$where['month'] = date('m');
		$salepurposeList = HttpDaoManager::getSalepurposeDao()->searchList($where);

		$salepurpseInfo = array();
		if(!empty($salepurposeList)){
			$salepurpseInfo = $salepurposeList[0];
		}
		
		$data['salePurpose'] = $salepurpseInfo['sale_purpose'];
		$data['registerPurpose'] = $salepurpseInfo['register_purpose'];
		$data['registerPayPurpose'] = $salepurpseInfo['register_pay_purpose'];

		$where = array();
		$where['start_time']=date('Y-m-'.'01');
		$where['pay_status']=PAY_STATUS_2;
		$data['saleComplete'] = HttpDaoManager::getOrderDao()->getSumOrderAmount($where);
		$data['saleUncomplete'] = $data['salePurpose']*10000-$data['saleComplete']['sumamount'];
		
		
		$where = array();
		$where['start_time']=date('Y-m-'.'01');
		$registerUserList = HttpDaoManager::getUserDao()->getSumUserRegister($where);
		$data['registerNoaction'] = 0;
		$data['registerExperience'] = 0;
		$data['registerMoney'] = 0;		
		if(!empty($registerUserList)){
			foreach($registerUserList as $key=>$value){
				if($value['user_type']==USER_TYPE_NOACTION){
					$data['registerNoaction'] = $value['cnt'];
				}
				
				if($value['user_type']==USER_TYPE_EXPERIENCE_NOMONEY){
					$data['registerExperience'] = $value['cnt'];
				}
				
				if($value['user_type']==USER_TYPE_MONEY){
					$data['registerMoney'] = $value['cnt'];
				}
				
			}
		}
		$data['registerComplete']= $data['registerNoaction']+$data['registerExperience']+$data['registerMoney'];
		
		$data['registerUncomplete'] = $data['registerPurpose']-$data['registerComplete'];

		$data['registerPayUncomplete'] = $data['registerPayPurpose']-$data['registerMoney'];		
		return $data;
	}
	
	public function info(){
		$data = HttpDaoManager::getOrderDao()->get($_GET['id']);
		$data['orderStatus'] = Common::_t('order_status');
		$data['order_status_name'] = $data['orderStatus'][$data['order_status']];
		$data['user'] = HttpDaoManager::getUserDao()->get($data['user_id']);
		$employees = HttpDaoManager::getEmployeesDao()->get($data['user']['employee_id']);
		if(!empty($employees))
			$data['user']['employees'] = $employees['name'];
		
		unset($employees);
		$productIdList = explode('|',$data['product_id']);
		foreach($productIdList as $k=>$v){
			if(empty($v)){
				unset($productIdList[$k]);
			}
			if(!intval($v)){
				unset($productIdList[$k]);
			}
		}
		
		$data['productList'] = HttpDaoManager::getProductDao()->getDataPairList($productIdList);
		
		$data['pay_time'] = date('Y-m-d H:i:s',$data['pay_time']);
		$payStatus = Common::_t('pay_status');
		$data['pay_status']=$payStatus[$data['pay_status']];
		$payType = Common::_t('pay_type');
		$data['pay_type'] = $payType[$data['pay_type']];
		$data['log'] = HttpDaoManager::getOrderLogDao()->searchList(array('order_id'=>$data['id']));
		$data['productType'] = Common::_t('product_type');
		$data['book_log_type'] = Common::_t('order_log_type');
		
		return $data;
	}
	/**
	 * 保存备注
	 */
	public function saveRemark(){
		$id = isset($_GET['id'])?$_GET['id']:$_POST['id'];
		HttpServerManager::getOrderServer()->saveRemark($id,$_POST['remark']);
		$msg = Common::_t('msg');
		ClassManager::getToos()->msgbox($msg[MESSAGE_TYPE_7]);
	}
	
	//确认
	public function confirm(){
		$id = isset($_GET['id'])?$_GET['id']:$_POST['id'];
		HttpServerManager::getOrderServer()->confirm($id,$_POST['remark']);
		ClassManager::getToos()->msgbox(Common::_t('data_operation_success'));
	}
	//完成
	public function complete(){
		$id = isset($_GET['id'])?$_GET['id']:$_POST['id'];
		HttpServerManager::getOrderServer()->complete($id,$_POST['remark']);
		ClassManager::getToos()->msgbox(Common::_t('data_operation_success'));
	}
	//取消
	public function cancellation(){
		$id = isset($_GET['id'])?$_GET['id']:$_POST['id'];
		HttpServerManager::getOrderServer()->cancel($id,$_POST['remark']);
		ClassManager::getToos()->msgbox(Common::_t('data_operation_success'));
	}

	public function getOrder(){
		$target = $_POST['target'];
		$where = array();
		$where['order_no'] = $target;
		$orderList = HttpDaoManager::getOrderDao()->searchList($where);
	
		$result= array();
		if(empty($orderList)){//找不到
			$result = array('success'=>'fail','num'=>0)	;
		}else{//找到了
			$num = count($orderList);
			if($num>1){
				$result = array('success'=>'fail','num'=>$num);
			}
			$result = array('success'=>'success','num'=>$num,'order_info'=>$orderList[0]);
		}
	
		exit(json_encode($result));
	}	
}
?>